package com.hopes.bizz.video.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.hopes.bizz.video.hikSync.bo.NodesByParamsRequest;
import com.hopes.bizz.video.hikSync.bo.ResourcesByParamsRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author daijie
 * @version 1.0
 * @date 2023/10/24 15:06
 * @Description:
 */
@Component
@Slf4j
public class HikSyncConfig {
    @Value("${hik.host:}")
    private String host;
    @Value("${hik.appKey:}")
    private String appKey;
    @Value("${hik.appSecret:}")
    private String appSecret;

    @PostConstruct
    public void init() {
        // artemis网关服务器ip端口
        ArtemisConfig.host = host;
        // 秘钥 appKey
        ArtemisConfig.appKey = appKey;
        // 秘钥 appSecret
        ArtemisConfig.appSecret = appSecret;
    }

    // 海康威视
    private static final String ARTEMIS_PATH = "/artemis";

    /**
     * 同步区域信息
     */
    public JSONArray hikSyncRegion(String regionIndex) {
        int pageNo = 1;
        int pageSize = 1000;
        return getRegionDataFromHik(pageNo, pageSize, regionIndex);
    }

    /**
     * 从海康威视服务器获取区域信息
     */
    private JSONArray getRegionDataFromHik(int pageNo, int pageSize, String regionIndex) {
        JSONArray regionArray = new JSONArray();

        NodesByParamsRequest nodesByParamsRequest = new NodesByParamsRequest();
        nodesByParamsRequest.setPageNo(pageNo);
        nodesByParamsRequest.setPageSize(pageSize);
        nodesByParamsRequest.setResourceType("camera");
        ArrayList<String> parentIndexCodeList = new ArrayList<>();
        if (null == regionIndex) {
            parentIndexCodeList.add("root000000");
        } else {
            parentIndexCodeList.add(regionIndex);
        }
        nodesByParamsRequest.setParentIndexCodes(parentIndexCodeList);

        String nodesByParamsDataApi = ARTEMIS_PATH + "/api/irds/v1/region/nodesByParams";
        Map<String, String> path = new HashMap<>(2) {
            {
                put("https://", nodesByParamsDataApi);
            }
        };
        String body = JSON.toJSONString(nodesByParamsRequest);
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, "application/json");
        if (result != null) {
            JSONObject resultO = JSONObject.parseObject(result);
            if (resultO.getString("msg").equals("success")) {
                JSONObject dataO = resultO.getJSONObject("data");
                regionArray = dataO.getJSONArray("list");

                int totalNo = dataO.getIntValue("total");
                if (totalNo > (pageNo * pageSize)) {
                    regionArray.addAll(this.getRegionDataFromHik((pageNo + 1), pageSize, regionIndex));
                }
            }
        }

        return regionArray;
    }

    /**
     * 获取海康
     *
     * @param regionIndex  区域编码
     * @param resourceType 资源类型
     * @return
     */
    public JSONArray listHikResource(String regionIndex, String resourceType) {
        int pageNo = 1;
        int pageSize = 1000;
        JSONArray result = this.getResourceDataFromHik(pageNo, pageSize, regionIndex, resourceType);
        System.out.println(result);
        return result;
    }

    private JSONArray getResourceDataFromHik(int pageNo, int pageSize, String regionIndex, String resourceType) {
        JSONArray siteArray = new JSONArray();

        ResourcesByParamsRequest resourcesByParamsRequest = new ResourcesByParamsRequest();
        resourcesByParamsRequest.setPageSize(pageSize);
        resourcesByParamsRequest.setPageNo(pageNo);
        if (null != regionIndex) {
            ArrayList<String> list = new ArrayList<>();
            list.add(regionIndex);
            resourcesByParamsRequest.setRegionIndexCodes(list);
        }
        resourcesByParamsRequest.setResourceType(resourceType);

        String resourcesByParamsDataApi = ARTEMIS_PATH + "/api/irds/v2/resource/resourcesByParams";
        Map<String, String> path = new HashMap<>(2) {
            {
                put("https://", resourcesByParamsDataApi);
            }
        };
        String body = JSON.toJSONString(resourcesByParamsRequest);
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, "application/json");
        if (result != null) {
            JSONObject resultO = JSONObject.parseObject(result);
            if (resultO.getString("msg").equals("success")) {
                JSONObject dataO = resultO.getJSONObject("data");
                siteArray = dataO.getJSONArray("list");

                int totalNo = dataO.getIntValue("total");
                if (totalNo > (pageNo * pageSize)) {
                    siteArray.addAll(this.getResourceDataFromHik((pageNo + 1), pageSize, regionIndex, resourceType));
                }
            }
        }

        return siteArray;
    }
}
